<?php

namespace XWAM\Addon\POrgDocument\Controller;

/**
 * DocumentController类
 *
 * @since 1.0 <2015-10-22> SoChishun <14507247@qq.com> Added.
 */
class DocumentsController extends AddonbaseController {

    function document_list_search_data() {
        $search = $_GET;
        $where = array();
        if (!empty($search['search_key'])) {
            $where['title'] = array('like', '%' . $search['search_key'] . '%');
        }
        if (!empty($search['category_id'])) {
            $where['category_id'] = $search['category_id'];
        }
        if (!empty($search['title'])) {
            $where['title'] = array('like', '%' . $search['title'] . '%');
        }
        return array('search' => $search, 'where' => $where);
    }

    function document_list() {
        $search_data = $this->document_list_search_data();
        $list = AddonD('Document')->get_paging_list($search_data['where'], $show, array('ORDER'=>'sort, id desc','PAGEPARAMS' => $search_data['search']));
        $list2 = false;
        if ($list) {
            $channels = AddonD('DocumentCategory')->where(array('type_id' => 2))->cache(20)->getField('id, title');
            foreach ($list as $row) {
                $row['category_name'] = isset($channels[$row['category_id']]) ? $channels[$row['category_id']] : '';
                $list2[] = $row;
            }
        }
        $category_id = I('category_id');
        $this->assign('meta_title', $category_id ? AddonD('DocumentCategory')->where(array('id' => $category_id))->getField('title') : '文章管理');
        $this->assign('list', $list2);
        $this->assign('page', $show);
        $this->assign('search', $search_data['search']);
        $this->display_cpp();
    }

    function document_edit($id = 0, $category_id = 0) {
        $data = array();
        if ($id) {
            $data = AddonD('Document')->find($id);
            $data['content'] = AddonD('DocumentContent')->get_content($id);
        }
        if ($category_id) {
            $data['category_id'] = $category_id;
        }
        $this->assign('meta_title', $id ? '编辑' . $data['title'] : '新增文章');
        $this->assign('data', $data);
        $this->display();
    }

    function document_edit_save() {
        $result = AddonD('Document')->save_document();
        $this->dialogJump($result['status'], $result['info']);
    }
    
    function change_status($id, $status){
        $this->ajaxReturn(AddonD('Document')->change_status($id,$status));
    }
    function change_sort($id, $sort){
        $this->ajaxReturn(AddonD('Document')->change_sort($id,$sort));
    }

    function document_delete($id = '') {
        $result = AddonD('Document')->delete_document($id);
        $this->dialogJump($result['status'], $result['info']);
    }

}
